<?php
/**
* @Copyright Copyright (C) 2010- ... Andy Sharman
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

$doc =& JFactory::getDocument();
$doc->addStyleSheet(JURI::base(false).'/components/com_customfields/customfields.css' );

if (JRequest::getVar('txtFieldName'))
{
	$CF->addField(JRequest::getString('txtFieldName'),JRequest::getString('cboFieldPosition'));
}
?>
	<div id="addCustomField">
		<form name="frmAddField" id="frmAddFields" method="post" action="">
			<p><label>Field Name :: </label><input class="textbox" type="text" name="txtFieldName" value="" /></p>
			<p>
				<label>Field Position :: </label>
				<select name="cboFieldPosition">
					<option value="module" selected="selected">Show in Module</option>
					<option value="onAfterDisplayTitle">After Title before main body</option>
					<option value="onAfterDisplayContent">After all content</option>
				</select>
			</p>
			<p>
				<input type="submit" class="btnAdd" name="btnSave" value="Save" />
			</p>
		</form>
		<div class="clear"></div>
	</div>
	<div class="clear"></div>
	
<?php

if (JRequest::getVar('updateId'))
{
	$CF->updateField(JRequest::getString('updateId'),
						JRequest::getString('txtUpdateFieldName'),
						JRequest::getString('cboUpdateFieldPosition'));
}

if (JRequest::getVar('deleteId'))
{
	$CF->deleteField(JRequest::getString('deleteId'));
}

if ($CF->countFields() > 0)
{
	$fields = $CF->listFields();
	
	foreach ($fields as $field)
	{
		echo '<div class="customFieldListItem">';
			echo '<form name="frmUpdateField'.$field->id.'" class="updateForm" method="post" action="">';
				echo '<input type="hidden" name="updateId" value="'.$field->id.'" />';
				echo '<p><label>Name :: </label><input class="textbox" type="text" name="txtUpdateFieldName" value="'.$field->name.'" /></p>';
				echo '<p><label>Position :: </label>';
				echo '<select name="cboUpdateFieldPosition">';
					echo '<option value="module"';
					if ($field->position == 'module') { echo ' selected="selected"'; }
					echo '>Show in Module</option>';
					echo '<option value="onAfterDisplayTitle"';
					if ($field->position == 'onAfterDisplayTitle') { echo ' selected="selected"'; }
					echo '>After Title before main body</option>';
					echo '<option value="onAfterDisplayContent"';
					if ($field->position == 'onAfterDisplayContent') { echo ' selected="selected"'; }
					echo '>After all content</option>';
				echo '</select></p>';
				echo '<p><input type="submit" class="btnAmend" name="btnUpdate" value="Update" />';
			echo '</form>';
			echo '<form name="frmDeleteField'.$field->id.'" class="delete" method="post" action="">';
				echo '<input type="hidden" name="deleteId" value="'.$field->id.'" />';
				echo '<input type="submit" class="btnDelete" name="btnDelete" value="Delete" /></p>';
			echo '</form>';
		echo '<div class="clear"></div></div><div class="clear"></div>';	
	}
	
}